<script setup lang="ts">
import { ref } from "vue";
import { formRules } from "../utils/rule";
import { LabelFormProps } from "../utils/types";
import { useLabelm } from "../utils/hook";

const { treeData } = useLabelm();
const options = ref([]);
if (treeData.value && treeData.value.length > 0) {
  options.value = treeData.value[0].children;
}

const props = withDefaults(defineProps<LabelFormProps>(), {
  formInline: () => ({
    id: "",
    itemName: "",
    unitId: undefined
  })
});

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="130px"
    class="mr-6"
  >
    <el-form-item label="单位分类" prop="unitId">
      <el-select v-model="newFormInline.unitId" clearable class="!w-[full]">
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.unitName"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="计量单位名称" prop="itemName">
      <el-input
        v-model="newFormInline.itemName"
        clearable
        placeholder="请输入单位名称"
      />
    </el-form-item>
  </el-form>
</template>
